package Presenter;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import Models.FGIInventory;
import Models.Firkins;
import Models.InvRow;

public class FGIInventoryPresenter extends AbstractPresenter<FGIInventory> {
	final String SINGLEITEM = "inventoryReport";
	final String MULTIITEM = "inventoryReports";
	
	public ArrayList<FGIInventory> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public FGIInventory tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public FGIInventory createItemFromJSONObject(JSONObject jsonObject){
		FGIInventory item = new FGIInventory();
		try{
			//Set inProductionBeers Collection
			ArrayList<InvRow> inProRows = new ArrayList<InvRow>();
			if(!jsonObject.isNull("inProductionBeers")){
				Object itm = jsonObject.get("inProductionBeers");
				InvRowPresenter presenter = new InvRowPresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						inProRows.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					inProRows.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setInProductionBeers(inProRows);
			}
			
			//Set nonProductionBeers Collection
			ArrayList<InvRow> nonProRows = new ArrayList<InvRow>();
			if(!jsonObject.isNull("nonProductionBeers")){
				Object itm = jsonObject.get("nonProductionBeers");
				InvRowPresenter presenter = new InvRowPresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						nonProRows.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					nonProRows.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setNonProductionBeers(nonProRows);
			}
			
			//Set firkinInventory Collection
			ArrayList<Firkins> firkins = new ArrayList<Firkins>();
			if(!jsonObject.isNull("firkinInventory")){
				Object itm = jsonObject.get("firkinInventory");
				FirkinPresenter presenter = new FirkinPresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						firkins.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					firkins.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setFirkinInventory(firkins);
			}
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(FGIInventory item){
		try{
			JSONObject json = new JSONObject();
			
			//Create Doc Collection
			if(item.getInProductionBeers()!=null){
				InvRowPresenter presenter = new InvRowPresenter();
				if(item.getInProductionBeers().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getInProductionBeers().size(); i++){
						jsonArray.put(presenter.createJSON(item.getInProductionBeers().get(i)));
					}
					json.put("inProductionBeers", jsonArray);
				}
			}
			
			//Create Doc Collection
			if(item.getNonProductionBeers()!=null){
				InvRowPresenter presenter = new InvRowPresenter();
				if(item.getNonProductionBeers().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getNonProductionBeers().size(); i++){
						jsonArray.put(presenter.createJSON(item.getNonProductionBeers().get(i)));
					}
					json.put("nonProductionBeers", jsonArray);
				}
			}
			
			//Create Doc Collection
			if(item.getFirkinInventory()!=null){
				FirkinPresenter presenter = new FirkinPresenter();
				if(item.getFirkinInventory().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getFirkinInventory().size(); i++){
						jsonArray.put(presenter.createJSON(item.getFirkinInventory().get(i)));
					}
					json.put("firkinInventory", jsonArray);
				}
			}
			
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public FGIInventory getEmptyEntity(){
		FGIInventory entity = new FGIInventory();
		return entity;
	}
}
